import Tkinter
import Widgets
import Handler

class Element(object):
    def __init__(self, factory, metaWidget=None, metaHandler=None):
        self.factory     = factory
        self.metaWidget  = metaWidget
        self.metaHandler = metaHandler

    def createHandler(self, widget):
        if self.metaHandler:
            newHandler = self.metaHandler(self.factory, widget)
            self.factory.widgets[widget] = newHandler
            return newHandler
        return None

    def createWidget(self, parent):
        if not self.metaWidget == None:
            widget = self.metaWidget(parent)
            return widget
        return None

class FactoryImpl(object):
    def __init__(self, controller):
        self.controller = controller
        self.data       = {}
        self.handler    = {}
        self.models     = {}
        self.widgets    = {}

    def initializeModels(self):
        for model in self.models.values():
            model.initialize()

    def registerHandler(self, wid, handler):
        if not wid == None and not handler == None:
            self.handler[wid] = handler

            # has anybody registered for given widget id?
            if self.models.has_key(wid):
                handler.addModel(self.models[wid])

            # No!
            else:
                parentHandler = handler.getParent()

                while not parentHandler == None:
                    if self.models.has_key(parentHandler.wid):
                        handler.addModel(self.models[parentHandler.wid])
                        break

                    # next parent
                    parentHandler = parentHandler.getParent()

    def registerModel(self, wid, model):
        self.models[wid] = model
        model.setFactory(self)

    ## This is for registering required Tkinter elements
    #  for a name to be used as tag in the XML resource.
    def registerElements(self):
        element = Element(self, Widgets.Frame, Handler.FrameHandler)
        self.data["frame"] = element

        element = Element(self, Widgets.Canvas, Handler.CanvasHandler)
        self.data["canvas"] = element

        element = Element(self, Widgets.Button, Handler.ButtonHandler)
        self.data["button"] = element

        element = Element(self, Widgets.Label, Handler.LabelHandler)
        self.data["label"] = element

        element = Element(self, Widgets.Entry, Handler.EntryHandler)
        self.data["entry"] = element

        element = Element(self, Widgets.Checkbutton, Handler.CheckbuttonHandler)
        self.data["checkbutton"] = element

        element = Element(self, Widgets.Text, Handler.TextHandler)
        self.data["text"]  = element

        element = Element(self, Widgets.Listbox, Handler.ListboxHandler)
        self.data["listbox"] = element

        element = Element(self, Widgets.Radiobutton, Handler.RadiobuttonHandler)
        self.data["radiobutton"] = element

        element = Element(self, Widgets.Scale, Handler.ScaleHandler)
        self.data["scale"] = element

        element = Element(self, Widgets.LabelFrame, Handler.LabelFrameHandler)
        self.data["labelframe"] = element

        element = Element(self, Widgets.Menu, Handler.MenuHandler)
        self.data["menu"] = element

        element = Element(self, Widgets.Scrollbar, Handler.ScrollbarHandler)
        self.data["scrollbar"] = element

        element = Element(self, None, Handler.PackHandler)
        self.data["pack"] = element

        element = Element(self, None, Handler.BindHandler)
        self.data["bind"] = element
